package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import CommonGui.NotEdditibleTable;
import Messages.MessageGetReportWaitersByDate;
import Messages.MessageGetReportWaitersByDateReply;

/**
 * Class  for creation JTable with report on tables or on waiter
 */
public class PfReportOnTablesAndWaiter extends JPanel {
	
	
	private MainMenu mainMenu;
	private Vector<Vector> reportWaiters;	
	
	private JPanel setDateForSearch;
	private JPanel detailsPanel;
	
	private DefaultTableModel dtm;
	private JTable tbl;
	
	private Vector<String> selectedRowVec=null;
	
	private String globalDate=null;
	
	private JDatePickerImpl newOrderDate;
	private JDatePickerImpl dateForSearchFrom ;
	private JDatePickerImpl dateForSearchTo ;
	
	private JButton refreshBtn;	
	
	private String FromDate=null;
	private String ToDate=null;
	private String waiterID;
	
	private Dimension screenSize;
	
	private int x;
	private int y;
	
	private int flagReportFirstOrReportSecondOfThisWaiter;
	
	public PfReportOnTablesAndWaiter(MainMenu mm, Vector<Vector> reportWaiters,Integer flag)
	{
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.mainMenu=mm;
		this.reportWaiters=reportWaiters;
		this.flagReportFirstOrReportSecondOfThisWaiter=flag;
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Date date = new Date();
		globalDate=dateFormat.format(date);
		initiliaze();
		
	}	
	
	
	private void initiliaze()
	{
				
		if(flagReportFirstOrReportSecondOfThisWaiter==2)
		{
		JScrollPane scrollPane = new JScrollPane(getTbl1Report());
		this.add(scrollPane);
		}
		else
		{
			JScrollPane scrollPane = new JScrollPane(getTbl2Report());
			this.add(scrollPane);
		}
		this.add( getButtonsPanel());
	}
	
	
	/**
	 * Method for creation JTable tbl with report(statistical)on tables by range of date 
	 * @return JTable tbl
	 */
	private JTable getTbl1Report()
	{
		Vector <String> tblheader=new Vector<String>();
		
		tblheader.add("תאריך");
		tblheader.add("זמן");
		tblheader.add("מס' שולחן");
		tblheader.add("כמות לקוחות");
		tblheader.add("טעימות");
		tblheader.add("כמות היינות");
		tblheader.add("כמות שתיה קלה");
		tblheader.add("1");
				
		dtm=new NotEdditibleTable(reportWaiters,tblheader);	 		
		tbl = new JTable(dtm);
		tbl.getTableHeader().setReorderingAllowed(false);                         //Not move columns
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<5;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        TableColumn tm = tbl.getColumnModel().getColumn(0);
       // tbl.setDefaultRenderer(Object.class, new ColorRowRendererForTelOrder());
        					
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.removeColumn(tbl.getColumnModel().getColumn(7));
		tbl.removeColumn(tbl.getColumnModel().getColumn(6));
		tbl.removeColumn(tbl.getColumnModel().getColumn(5));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
       // tbl.getModel().addTableModelListener(this);
        
        TableColumn col_6 = tbl.getColumnModel().getColumn(2);
           
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			     if (e.getClickCount() == 1) {
			       JTable target = (JTable)e.getSource();
			       tbl.setColumnSelectionAllowed(true);        //Select only one cell
			       tbl.setRowSelectionAllowed(true);
			       int row = target.getSelectedRow();
			       int column = target.getSelectedColumn();
			       if(row!=-1)
			       {
				       selectedRowVec=(Vector<String>) dtm.getDataVector().get(row);
			       }
			     }
			     
			   		     
			   }
			   
			 });
        return tbl;
	}
	

	/**
	 * Method for creation JTable tbl with report(statistical)on waiter by range of date 
	 * @return JTable tbl
	 */
	private JTable getTbl2Report()
	{
		Vector <String> tblheader=new Vector<String>();
		tblheader.add("תאריך");
		tblheader.add("כמות שולחנות");
		tblheader.add("כמות לקוחות");
		tblheader.add("טעימות");
		tblheader.add("טעימות");
		tblheader.add("כמות היינות");
		tblheader.add("כמות שתיה קלה");
		tblheader.add("1");
		
		dtm=new NotEdditibleTable(reportWaiters,tblheader);	 		
		tbl = new JTable(dtm);
		tbl.getTableHeader().setReorderingAllowed(false);                         //Not move columns
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<5;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        TableColumn tm = tbl.getColumnModel().getColumn(0);
       // tbl.setDefaultRenderer(Object.class, new ColorRowRendererForTelOrder());
        					
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.removeColumn(tbl.getColumnModel().getColumn(7));
		tbl.removeColumn(tbl.getColumnModel().getColumn(6));
		tbl.removeColumn(tbl.getColumnModel().getColumn(5));
		tbl.removeColumn(tbl.getColumnModel().getColumn(4));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
       // tbl.getModel().addTableModelListener(this);
        
        TableColumn col_6 = tbl.getColumnModel().getColumn(2);
                     
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			     if (e.getClickCount() == 1) {
			       JTable target = (JTable)e.getSource();
			       tbl.setColumnSelectionAllowed(true);        //Select only one cell
			       tbl.setRowSelectionAllowed(true);
			       int row = target.getSelectedRow();
			       int column = target.getSelectedColumn();
			       if(row!=-1)
			       {
				       selectedRowVec=(Vector<String>) dtm.getDataVector().get(row);
			       }
			     }
			    
			   }
			   
			 });
                
        return tbl;
	}
	
	
	
	
	//==================================================================================================================================================	
	
	
	

    /**
     * Method for creation JPanel  buttonsPanel and gets two buttons(print and cancel)
     *  from methods getBtnPrnt and getBtnCancel
     * 
     * @return buttonsPanel
     */
	private JPanel getButtonsPanel()
	{
		JPanel buttonsPanel=new JPanel();
		buttonsPanel.setBackground(new Color(255, 250, 205));
		buttonsPanel.setForeground(new Color(0, 0, 0));
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//buttonsPanel.add(getBtnPrnt());
		buttonsPanel.add(getBtnCancel());
		return buttonsPanel;
	}
	
	/**
	 * Method for creation JButton btnPrt for printing of report
	 * @return JButton btnPrt
	 */
	private JButton getBtnPrnt()
	{
		JButton btnPrt=new JButton("הדפס");
		btnPrt.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnPrt.setPreferredSize(new Dimension(x/10,y/9));
		btnPrt.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				
		
			}
		});
		
		return btnPrt;
	}
	
	/**
	 * Method for creation JButton btnCancel for return to preview screen
	 * @return JButton btnCancel
	 */
	private JButton getBtnCancel()
	{
		
		JButton btnCancel=new JButton("סגור");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCancel.setPreferredSize(new Dimension(x/10,y/9));
		btnCancel.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{		
				((JButton)e.getSource()).getParent().getParent().getParent().remove(((JButton)e.getSource()).getParent().getParent());
				mainMenu.getContentPane().validate();
			}
		});
		return btnCancel;
	}
	
	
	
	
	
	
	
}

